package action.student;

import beans.Assignment;
import dao.MngAssignment;
import dao.Student;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class addStudent extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String acc_id = request.getParameter("acc_id");
        String class_id = request.getParameter("class_id");
        MngAssignment mngAssign = new MngAssignment();
        List<Assignment> listAssign = mngAssign.listAssignByClass(class_id);
        Student st = new Student();
        String msg = "";
        String error = "";
        if (st.addStudentinClass(acc_id, class_id)) {
            for (Assignment assign : listAssign) {
                assign.setAcc_id(acc_id);
                mngAssign.addAssignForNewStudent(assign);
            }
            msg = "Join successful !";
        } else {
            error = "Join fail !";
        }
        request.setAttribute("msg", msg);
        request.setAttribute("error", error);
        RequestDispatcher rd = request.getRequestDispatcher("student.jsp?page=listClass");
        rd.forward(request, response);
    }
}
